package cn.msn.user.controller;

import cn.msn.basic.exception.BusinessException;
import cn.msn.basic.util.AjaxResult;
import cn.msn.user.dto.LoginDto;
import cn.msn.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
		@RequestMapping("/login")
		public class LoginController {

		    @Autowired
		    private ILogininfoService logininfoService;

		   
		    @PostMapping("/account")
		    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
		        try {
		            //map中有两个信息：token - 做登录拦截， 登录信息对象 - 显示用户名(清空密码和盐值)
		            Map<String,Object> map =  logininfoService.accountLogin(loginDto);
		            return AjaxResult.me().setResultObj(map);
		            //只要有业务异常都要加
		        } catch (BusinessException e) {
		            e.printStackTrace();
		            return new AjaxResult(false,e.getMessage());
		        } catch (Exception e) {
		            e.printStackTrace();
		            return new AjaxResult(false,"登录失败");
		        }
		    }

	@PostMapping("/wechat")
	public AjaxResult wechatLogin(@RequestBody LoginDto loginDto){
		try {
			return logininfoService.wechatLogin(loginDto.getCode());
			//只要有业务异常都要加
		} catch (BusinessException e) {
			e.printStackTrace();
			return new AjaxResult(false,e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false,"登录失败");
		}
	}

	@PostMapping("/wechat/binder")
	public AjaxResult wechatBinder(@RequestBody Map<String, String> map) {
		try {
			return logininfoService.wechatBinder(map);
		} catch (BusinessException e) {
			e.printStackTrace();
			return new AjaxResult(false,e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false,"登录失败！");
		}
	}
}